<?php

namespace app\api\controller;

use app\common\controller\Api;
// use app\admin\model\SurprisedGoods;
use think\Db;
/**
 *  惊喜订单
 *
 *  Author:  YSX
    Date:    2022-6-9
 */
class Integralorder extends Api
{
    // protected $noNeedLogin = ['goodsList'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }
	//如果是套房的需要选择日历
	public function calendar(){
		// $params = $this->params;
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		if($params['month']){
			$month = $params['month'];
		}else{
			$month = date('Y-m');
		}
		$where['goods_id'] = array('eq',$params['goods_id']);
		$where['is_del'] = array('eq',1);
		$where['state'] = array('eq',3);
		$where['goods_attr_id'] = array('eq',$params['goods_attr_id']);
		
		$firstday = date('Y-m-01', strtotime($month));
		$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
		$where['starttime'] = array('between',[strtotime($month),strtotime($lastday)]);
		$res['choice'] = Db::name('integral_order_goods')->field('id,starttime')->where($where)->select();
		// halt(Db::name('integral_order_goods')->getLastSql());
		$res['holiday'] = Db::name('holiday')->where('startday','>',time())->select();
		$this->success('获取成功', $res);
	}
    /*
     * 确认订单接口
    */
    public function confirm()
    {
        // $params = $this->params;
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		//获取当前人的积分
		$integral = $this->auth->integral;
		if(isset($params['cart_id'])){
			//从购物车下单
			$data = Db::name('integral_cart as a')->where('a.id','in',$params['cart_id'])
				->join('integral_goods x', 'a.integral_goods_id = x.id')
				->join('integral_goods_attr o', 'a.integral_goods_attr_id = o.id')
			    ->field('a.id,a.number,a.state,x.goods_name,x.image,x.price,x.mix_title,x.lineation_price,x.tips,o.attr_name,o.image as attr_image,o.attr_name,o.price as attr_price')
			    ->order('a.id desc')
			    ->select(); 
			$totalMoney = 0;
			foreach($data as $key => $value){
				//用属性的价格
				$data[$key]['payMoney'] = $value['attr_price'] * $value['number'];
				$totalMoney += $data[$key]['payMoney'];
			}
			//判断目前这个人积分是否购买这个商品
			if($integral < $totalMoney){
				$this->error('积分不足');
			}
			if ($data) {
				$this->success('获取成功', $data);
			}
			$this->error('请求失败');
		}else{
			//直接支付去下单
			$where['is_del'] = array('eq',1);
			$where['is_enable'] = array('eq',1);
			$where['id'] = array('eq',$params['integral_goods_id']);
			
			$goodsInfo = Db::name('integral_goods')->field('id,goods_name,image,price,state,tips')->where($where)->find();
			$goodsAttrInfo = Db::name('integral_goods_attr')->where('id',$params['integral_goods_attr_id'])->field('attr_name,price,image')->find();
			$goodsInfo['attr_name'] = $goodsAttrInfo['attr_name'];
			$goodsInfo['attr_image'] = $goodsAttrInfo['image'];
			$goodsInfo['attr_price'] = $goodsAttrInfo['price'];
			$goodsInfo['payMoney'] = $params['number'] * $goodsAttrInfo['price'];
			//判断目前这个人积分是否购买这个商品
			if($integral >= $goodsInfo['payMoney']){
				if ($goodsInfo) {
					$this->success('获取成功', $goodsInfo);
				}
			}else{
				$this->error('积分不足');
			}
			
			$this->error('请求失败');
		}
    }
	/*
	 * 下单接口
	*/
	public function confirmOrder()
	{
	    // $params = $this->params;
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		$userid = $this->auth->id;
		if(isset($params['cart_id'])){
			//判断积分是否充足
			$integral = $this->auth->integral;
			
			//从购物车下单
			$goodsInfo = Db::name('integral_cart as a')->where('a.id','in',$params['cart_id'])
				->join('integral_goods x', 'a.integral_goods_id = x.id')
				->join('integral_goods_attr o', 'a.integral_goods_attr_id = o.id')
				->field('a.id,a.integral_goods_id,a.state,a.integral_goods_attr_id,a.number,x.goods_name,x.image,x.mix_title,x.lineation_price,x.tips,o.price,o.attr_name')
				->order('a.id desc')
				->select(); 
			$pay_money = 0;
			foreach($goodsInfo as $key => $value){
				$pay_money += $value['price'] * $value['number'];
			}
			if($integral < $pay_money){
				$this->error('积分不足');
			}
			//查找用户地址信息
			$addressInfo = Db::name('member_address')->where('id',$params['address_id'])->find();
			//生成订单号
			$order_no = date('YmdHis', time()) . str_pad(mt_rand(1, 99999), 5, 0, STR_PAD_LEFT) . rand(1000, 99999);
			// \var_dump($pay_money);die;
			//启动事务
			Db::startTrans();
			try {
				$data = [
					'order_no' => $order_no,
					'member_id' => $userid,
					'mobile' => $addressInfo['mobile'],
					'username' => $addressInfo['username'],
					'province' => $addressInfo['province'],
					'city' => $addressInfo['city'],
					'county' => $addressInfo['county'],
					'address' => $addressInfo['address'],
					'housenumber' => $addressInfo['housenumber'],
					'address_id' => $params['address_id'],
					'pay_money' => $pay_money,
					'status' => 2,
					'state' => $params['state'],
					'pay_status' => 2,
					'pay_time' => time(),
					'create_time' => time(),
				];
				$order_id = Db::name('integral_order')->insertGetId($data);
				//从购物车下单
				$goodsInfo = Db::name('integral_cart as a')->where('a.id','in',$params['cart_id'])
					->join('integral_goods x', 'a.integral_goods_id = x.id')
					->join('integral_goods_attr o', 'a.integral_goods_attr_id = o.id')
					->field('a.id,a.integral_goods_id,a.state,a.integral_goods_attr_id,a.number,x.goods_name,x.image,o.price,o.attr_name')
					->order('a.id desc')
					->select(); 
				$pay_money = 0;
				$goods_data = [];
				foreach($goodsInfo as $key => $value){
					$goods_data[$key]['order_id'] = $order_id;
					$goods_data[$key]['goods_id'] = $value['integral_goods_id'];
					$goods_data[$key]['goods_attr_id'] = $value['integral_goods_attr_id'];
					$goods_data[$key]['attr_name'] = $value['attr_name'];
					$goods_data[$key]['goods_name'] = $value['goods_name'];
					$goods_data[$key]['image'] = $value['image'];
					$goods_data[$key]['price'] = $value['price'];
					$goods_data[$key]['number'] = $value['number'];
					$goods_data[$key]['state'] = $value['state'];
					$goods_data[$key]['create_time'] = time();
					// $pay_money += $value['price'] * $value['number'];
				}
				
				Db::name('integral_order_goods')->insertAll($goods_data);
				//修改支付总价
				// Db::name('integral_order')->where('id',$order_id)->update(['pay_money'=>$pay_money]);
				
				// 提交事务
				Db::commit();
				//删除购物车信息晚点再写
				Db::name('integral_cart')->where('id','in',$params['cart_id'])->delete();
				$res['order_no'] = $order_no;
				$res['pay_money'] = $pay_money;
			}catch (\Exception $e) {
				// \var_dump($e);
			    // 回滚事务
			    Db::rollback();
			}	
			if(isset($res['order_no'])){
				// $order =Db::name('integral_order')->where(['order_no' => $order_no])->find();
				//增加支付记录
				$p_data['member_id'] = $userid;
				$p_data['mobile'] = $this->auth->mobile;
				$p_data['score'] = $pay_money;
				$p_data['type'] = 2;
				$p_data['content'] = '购买积分商品';
				$p_data['integral'] = $integral - $pay_money;
				$p_data['createtime'] = $_SERVER['REQUEST_TIME'];
				$res = Db::name('member_integral_record')->insert($p_data);
				if($res){
					//修改个人积分
					Db::name('member')->where('id',$userid)->update(['integral'=>$p_data['integral']]);
				}
				$this->success('支付成功');
			}else{
				$this->error('支付失败');
			}
		}else{
			//查找商品信息
			$goodsInfo = Db::name('integral_goods')->field('id,goods_name,image,price')->where('id',$params['integral_goods_id'])->find();
			$goodsAttrInfo = Db::name('integral_goods_attr')->where('id',$params['integral_goods_attr_id'])->field('attr_name,price')->find();
			// $attr_name = Db::name('integral_goods_attr')->where('id',$params['goods_attr_id'])->value('attr_name');
			//判断积分是否充足
			$integral = $this->auth->integral;
			$payIntegral = $goodsAttrInfo['price'] * $params['number'];
			//生成订单号
			$order_no = date('YmdHis', time()) . str_pad($params['integral_goods_id'], 5, 0, STR_PAD_LEFT) . rand(1000, 99999);
			//判断是否是套房是的话得判断未完成的订单
			// if($params['state'] == 3){
			// 	//查找未完成的订单
			// 	$dates = explode(',',$params['dates']);
			// 	$info = [];
				
			// 	foreach($dates as $key =>$value){
			// 		$isFind = Db::name('integral_order_goods')->where('is_del',1)->where('goods_attr_id',$params['integral_goods_attr_id'])->where('state',3)->where('starttime','=',strtotime($value))->find();
			// 		if($isFind){
			// 			//把已经存在的房间保存一下然后出参
			// 			$info[] = $value;
			// 		}
			// 	}
			// 	// halt($info);
			// 	if(count($info) > 0){
			// 		$this->error(implode('、',$info).'房间已经被占用');
			// 	}
			// }else{
			// 	if($integral < $payIntegral){
			// 		$this->error('积分不足');
			// 	}
			// }
			//启动事务
			Db::startTrans();
			try {
				
				//判断是否是套房是的话得判断未完成的订单
				if($params['state'] == 3){
					//查找未完成的订单
					$dates = explode(',',$params['dates']);
					$holiday = explode(',',$params['holiday']);
					$pay_money = 0;
					foreach($dates as $key =>$value){
						//如果是节假日翻倍
						$pay_money += $goodsAttrInfo['price'] * $holiday[$key];
					}
					// $pay_money = $goodsAttrInfo['price'] * count($dates);
					if($integral < $pay_money){
						//代表积分不足
						$is_adequate = 2;
						$actual_pay_integral = $integral;
						$surplus_integral = $pay_money - $integral;
					}else{
						$is_adequate = 1;
						$actual_pay_integral = $pay_money;
						$surplus_integral = 0;
					}
					$data = [
					    'order_no' => $order_no,
					    'member_id' => $userid,
						'mobile' => $this->auth->mobile,
						// 'username' => $addressInfo['username'],
						// 'province' => $addressInfo['province'],
						// 'city' => $addressInfo['city'],
						// 'county' => $addressInfo['county'],
						// 'address' => $addressInfo['address'],
						// 'housenumber' => $addressInfo['housenumber'],
					    // 'address_id' => $params['address_id'],
					    'pay_money' => $pay_money,
					    'is_adequate' => $is_adequate,
					    'actual_pay_integral' => $actual_pay_integral,
					    'surplus_integral' => $surplus_integral,
					    'status' => 4,
					    'state' => $params['state'],
					    'pay_status' => 2,
					    'pay_time' => time(),
					    'create_time' => time(),
					];
					$order_id = Db::name('integral_order')->insertGetId($data);
					foreach($dates as $key =>$value){
						//添加订单商品表
						$goods_data = [
						    'order_id' => $order_id,
						    'goods_id' => $goodsInfo['id'],
						    'goods_attr_id' => $params['integral_goods_attr_id'],
						    'attr_name' => $goodsAttrInfo['attr_name'],
						    'goods_name' => $goodsInfo['goods_name'],
						    'image' => $goodsInfo['image'],
						    'price' => $goodsAttrInfo['price'] * $holiday[$key],
						    'number'=>$params['number'],
						    'state'=>$params['state'],
						    'holiday'=>$holiday[$key],
						    'create_time' => time(),
						    'starttime' => strtotime($value),
						];
						Db::name('integral_order_goods')->insert($goods_data);
					}
					
				}else if($params['state'] == 5){
					//线上问诊  医疗包年卡的东西
					// $dates = explode(',',$params['dates']);
					// \var_dump($pay_money);die;
					$data = [
					    'order_no' => $order_no,
					    'member_id' => $userid,
						'mobile' => $params['mobile'],
						'username' => $params['username'],
						// 'province' => $addressInfo['province'],
						// 'city' => $addressInfo['city'],
						// 'county' => $addressInfo['county'],
						'address' => $params['idCard'],
						// 'housenumber' => $addressInfo['housenumber'],
					    // 'address_id' => $params['address_id'],
					    'pay_money' => $goodsAttrInfo['price'],
						'actual_pay_integral' => $goodsAttrInfo['price'],
					    'status' => 4,
					    'state' => $params['state'],
					    'pay_status' => 2,
					    'pay_time' => time(),
					    'create_time' => time(),
					];
					$order_id = Db::name('integral_order')->insertGetId($data);
					//添加订单商品表
					$goods_data = [
						'order_id' => $order_id,
						'goods_id' => $goodsInfo['id'],
						'goods_attr_id' => $params['integral_goods_attr_id'],
						'attr_name' => $goodsAttrInfo['attr_name'],
						'goods_name' => $goodsInfo['goods_name'],
						'image' => $goodsInfo['image'],
						'price' => $goodsAttrInfo['price'],
						'number'=>$params['number'],
						'state'=>$params['state'],
						'create_time' => time(),
						// 'starttime' => strtotime($value),
					];
					Db::name('integral_order_goods')->insert($goods_data);
					
				}else if($params['state'] == 6){
					//线上问诊  医疗包年卡的东西
					// $dates = explode(',',$params['dates']);
					// \var_dump($pay_money);die;
					$data = [
					    'order_no' => $order_no,
					    'member_id' => $userid,
						'mobile' => $params['mobile'],
						// 'username' => $params['username'],
						// 'province' => $addressInfo['province'],
						// 'city' => $addressInfo['city'],
						// 'county' => $addressInfo['county'],
						// 'address' => $params['idCard'],
						// 'housenumber' => $addressInfo['housenumber'],
					    // 'address_id' => $params['address_id'],
					    'pay_money' => $goodsAttrInfo['price'],
						'actual_pay_integral' => $goodsAttrInfo['price'],
					    'status' => 4,
					    'state' => $params['state'],
					    'pay_status' => 2,
					    'pay_time' => time(),
					    'create_time' => time(),
					];
					$order_id = Db::name('integral_order')->insertGetId($data);
					//添加订单商品表
					$goods_data = [
						'order_id' => $order_id,
						'goods_id' => $goodsInfo['id'],
						'goods_attr_id' => $params['integral_goods_attr_id'],
						'attr_name' => $goodsAttrInfo['attr_name'],
						'goods_name' => $goodsInfo['goods_name'],
						'image' => $goodsInfo['image'],
						'price' => $goodsAttrInfo['price'],
						'number'=>$params['number'],
						'state'=>$params['state'],
						'create_time' => time(),
						// 'starttime' => strtotime($value),
					];
					Db::name('integral_order_goods')->insert($goods_data);
					
				}else{
					//查找用户地址信息
					$addressInfo = Db::name('member_address')->where('id',$params['address_id'])->find();
					// \var_dump($pay_money);die;
					$data = [
					    'order_no' => $order_no,
					    'member_id' => $userid,
						'mobile' => $addressInfo['mobile'],
						'username' => $addressInfo['username'],
						'province' => $addressInfo['province'],
						'city' => $addressInfo['city'],
						'county' => $addressInfo['county'],
						'address' => $addressInfo['address'],
						'housenumber' => $addressInfo['housenumber'],
					    'address_id' => $params['address_id'],
					    'pay_money' => $goodsAttrInfo['price'] * $params['number'],
						'actual_pay_integral' => $goodsAttrInfo['price'] * $params['number'],
					    'status' => 2,
					    'state' => $params['state'],
					    'pay_status' => 2,
					    'pay_time' => time(),
					    'create_time' => time(),
					];
					$order_id = Db::name('integral_order')->insertGetId($data);
					//添加订单商品表
					$goods_data = [
					    'order_id' => $order_id,
					    'goods_id' => $goodsInfo['id'],
					    'goods_attr_id' => $params['integral_goods_attr_id'],
					    'attr_name' => $goodsAttrInfo['attr_name'],
					    'goods_name' => $goodsInfo['goods_name'],
					    'image' => $goodsInfo['image'],
					    'price' => $goodsAttrInfo['price'],
					    'number'=>$params['number'],
					    'state'=>$params['state'],
					    'create_time' => time(),
					];
					Db::name('integral_order_goods')->insert($goods_data);
				}
				
			    // 提交事务
			    $result = Db::commit();
				$res['order_no'] = $order_no;
				// $res['pay_money'] = $data['pay_money'];
			    
			}catch (\Exception $e) {
				// \var_dump($e);
			    // 回滚事务
			    Db::rollback();
			    // $this->error('下单失败');
			}
			if(isset($res['order_no'])){
				$order = Db::name('integral_order')->where('order_no',$res['order_no'])->find();
				//增加支付记录
				$p_data['member_id'] = $userid;
				$p_data['mobile'] = $this->auth->mobile;
				$p_data['score'] = $order['actual_pay_integral'];
				$p_data['type'] = 2;
				$p_data['content'] = '购买积分商品';
				$p_data['integral'] = $integral - $order['actual_pay_integral'];
				$p_data['createtime'] = $_SERVER['REQUEST_TIME'];
				$res = Db::name('member_integral_record')->insert($p_data);
				if($res){
					//修改个人积分
					Db::name('member')->where('id',$userid)->update(['integral'=>$p_data['integral']]);
				}
				$this->success('支付成功');
			}else{
				$this->error('支付失败');
			}
		}
	}
	/*
	 * 我的订单接口
	*/
    public function myOrder(){
	   // $params = $this->params;
	    if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
	    }
	    $params = $this->request->post();
		$userid = $this->auth->id;
	    $where['member_id'] = array('eq',$userid);
	    $where['is_del'] = array('eq',1);
	    if(isset($params['status']) && !empty($params['status'])){
			$where['status'] = array('eq',$params['status']);
			// $where[] = ['coupon_code_class_id', '=' , ];
	    }
		$limit    = !empty($params['limit']) ? $params['limit'] : 10;
		$page     = !empty($params['page']) ? $params['page'] : 1;
	    $list = Db::name('integral_order')->field('id,order_no,create_time,pay_money,status,state,is_exchange')->page($page)->limit($limit)->where($where)->order('id desc')->select();
	    if($list){
		    foreach($list as $key => $value){
			    $list[$key]['gooodsInfo'] = Db::name('integral_order_goods')->field('id,goods_id,goods_attr_id,goods_name,attr_name,image,price,number,state,is_del,starttime,holiday')->where('order_id',$value['id'])->select();					
		    }
	    }
		$this->success('我的订单', $list);
    } 
	/*
	 * 订单详情
	*/
    public function orderDetail(){
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		$where['x.is_del'] = array('eq',1);
		if(isset($params['id']) && !empty($params['id'])){
			$where['x.id'] = array('eq',$params['id']);
		}
		//查找订单数据
		$info = Db::name('integral_order')
			->alias('x')
			// ->join('member_address s', 's.id = x.address_id','left')
			->join('areas a', 'x.province = a.id','left')
			->join('areas b', 'x.city = b.id','left')
			->join('areas c', 'x.county = c.id','left')
			->field('x.id,x.order_no,x.create_time,x.pay_money,x.status,x.address_id,x.username,x.mobile,x.address,x.housenumber,x.logistics_name,x.express_number,a.name as province_name,b.name as city_name,c.name as county_name')
			->where($where)->find();
		// halt($info);
		// halt(Db::name('integral_order')->getLastSql());
		$info['gooodsInfo'] = Db::name('integral_order_goods')->field('id,goods_id,goods_attr_id,goods_name,attr_name,image,price,number,state,starttime,is_del')->where('order_id',$params['id'])->select();		
		$this->success('订单详情', $info);
	}
	/*
	 * 取消订单
	*/
	public function cancelOrder(){
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		$where['is_del'] = array('eq',1);
		if(isset($params['id']) && !empty($params['id'])){
			$where['id'] = array('eq',$params['id']);
		}
		//取消订单
		$result = Db::name('integral_order')->where($where)->update(['status'=>5]);
		if ($result) {
			$this->success('取消成功');
		}
		$this->error('取消失败');
	}
	/*
	 * 删除订单
	*/
	public function delOrder(){
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		if(isset($params['id']) && !empty($params['id'])){
			$where['id'] = array('eq',$params['id']);
		}
		//取消订单
		$result = Db::name('integral_order')->where($where)->update(['is_del'=>2]);
		if ($result) {
			$this->success('删除成功');
		}
		$this->error('删除失败');
	}
	/*
	 * 确认收货
	*/
	public function confirmReceipt(){
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		if(isset($params['id']) && !empty($params['id'])){
			$where['id'] = array('eq',$params['id']);
		}
		$data['status'] = 4;
		$data['end_time'] = time();
		//把状态改成已完成
		$result = Db::name('integral_order')->where($where)->update($data);
		if ($result) {
			$this->success('收货成功');
		}
		$this->error('收货失败');
	}
	/*
	 * 换货
	*/
	public function exchange(){
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		if(isset($params['id']) && !empty($params['id'])){
			$where['id'] = array('eq',$params['id']);
		}
		$data['is_exchange'] = 2;
		$data['exchange_logistics'] = $params['exchange_logistics'];
		$data['exchange_number'] = $params['exchange_number'];
		$data['exchange_time'] = time();
		//把状态改成已完成
		$result = Db::name('integral_order')->where($where)->update($data);
		if ($result) {
			$this->success('已申请换货');
		}
		$this->error('换货失败');
	}
	/*
	 * 判断积分是否充足   购预定房间
	*/
	public function isAdequate(){
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		//查找未完成的订单
		$dates = explode(',',$params['dates']);
		//查找商品信息
		// $goodsInfo = Db::name('integral_goods')->field('id,goods_name,image,price')->where('id',$params['integral_goods_id'])->find();
		$goodsAttrInfo = Db::name('integral_goods_attr')->where('id',$params['integral_goods_attr_id'])->field('attr_name,price')->find();
		// $attr_name = Db::name('integral_goods_attr')->where('id',$params['goods_attr_id'])->value('attr_name');
		//判断积分是否充足
		$integral = $this->auth->integral;
		//判断所需积分
		$payIntegral = $goodsAttrInfo['price'] * count($dates);
		//判断剩余积分是否充足
		if($integral >= $payIntegral){
			$this->success('积分充足,可以预定');
		}else{
			//积分不足的话判断到预定日期中间差几个月 再加上这几个月的积分是否充足
			$num = $this->getMonthNum(date('Y-m-d'),$dates[count($dates)-1]);
			$surIntegral = $num * 100;
			$integral = $surIntegral + $integral;
			if($integral >= $payIntegral){
				$this->success('积分充足,可以预定');
			}else{
				$this->success('积分不足,不可以预定');
			}
		}
		// $this->error('换货失败');
	}
	public  function getMonthNum( $date1, $date2, $tags='-' ){ 
		$strtotime1 = strtotime($date1);
		$strtotime2 = strtotime($date2);
		$y = date('Y',$strtotime1);
		$ys = date('Y',$strtotime2);
		$m = (int)date('m',$strtotime1);
		$ms = (int)date('m',$strtotime2);
		$chaY = $ys - $y;
		//月份相差多少
		$chaM = 12 - $m + $ms;
		//相差一年就加12
		$yearmeth = $chaM + (($chaY-1) *12);
		return $yearmeth;
		// return abs($date1[0] - $date2[0]) * 12 + abs($date1[1] - $date2[1]); 
	}
	/*
	 * 退房接口
	*/
	public function checkOut(){
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		if(isset($params['id']) && !empty($params['id'])){
			$where['id'] = array('eq',$params['id']);
		}
		$isFind = Db::name('integral_order_goods')->where($where)->find();
		$times = $isFind['starttime'] - 7 * 86400;
		if($times > time()){
			//当天时间在订房时间七天之外说明可以退房
			$result = Db::name('integral_order_goods')->where($where)->update(['is_del'=>2]);
			if ($result) {
				//返回积分
				// $order = Db::name('integral_order')->where('order_no',$res['order_no'])->find();
				//增加支付记录
				$p_data['member_id'] = $this->auth->id;
				$p_data['mobile'] = $this->auth->mobile;
				$p_data['score'] = $isFind['price'];
				$p_data['type'] = 1;
				$p_data['content'] = '退房返还积分';
				$p_data['integral'] = $this->auth->integral + $isFind['price'];
				$p_data['createtime'] = $_SERVER['REQUEST_TIME'];
				$res = Db::name('member_integral_record')->insert($p_data);
				if($res){
					//修改个人积分
					Db::name('member')->where('id',$this->auth->id)->update(['integral'=>$p_data['integral']]);
				}
				$this->success('退房成功');
			}else{
				$this->error('退房失败');
			}
		}else{
			$this->error('七天之内不可以退房');
		}
	}
}
